package com.ldk.framework.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ldk.base.framework.common.CustomPageable;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.Driver;
import com.ldk.framework.entity.DriverReceiveTrip;
import com.ldk.framework.entity.PushMessage;
import com.ldk.framework.entity.ServiceTrip;
import com.ldk.framework.enums.DriverStatus;
import com.ldk.framework.enums.ServiceCode;
import com.ldk.framework.push.CustomMsgType;
import com.ldk.framework.push.PushCustomMsg;
import com.ldk.framework.push.PushTargetApp;
import com.ldk.framework.service.IDriverReceiveTripService;
import com.ldk.framework.service.IDriverService;
import com.ldk.framework.service.IPushMessageService;

/**
 * 快车用户行程推送给司机
 * @author xiangwl
 * 2017年3月24日
 * com.ldk.framework.service.impl
 * KCUserTripPushToDriverThread
 */
@Component
public class KCUserTripPushToDriverThread implements Runnable {
	private final Logger LOGGER = LoggerFactory.getLogger(ZXPCUserTripPushToDriverThread.class);
	/**
	 * 快车业务搜索最近的司机，把用户行程信息推送给司机
	 * 是否自定派单机制 放在外面判断
	 */
	
	
	/**
	 * 用户行程
	 */
	private ServiceTrip userTrip;
	
	@Autowired
	private IPushMessageService pushMessageService;
	@Autowired
	private IDriverService driverService;
	@Autowired
	private IDriverReceiveTripService driverReceiveTripService;
	
	public KCUserTripPushToDriverThread(ServiceTrip userTrip){
		this.userTrip = userTrip;
	}
	
	public KCUserTripPushToDriverThread(){
		
	}
	public void setUserTrip(ServiceTrip userTrip) {
		this.userTrip = userTrip;
	}

	@Override
	public void run() {
		try{
			CustomPageable pageable = new CustomPageable(0, 100);
			Driver driverQuery = new Driver();
			//driverQuery.setOpenKc(true);
			driverQuery.setEnabled(true);
			driverQuery.setServiceCodes(ServiceCode.KC.getCode());
			driverQuery.setLon(userTrip.getStartLon());
			driverQuery.setLat(userTrip.getStartLat());
			driverQuery.setStatus(DriverStatus.OnLine.getValue());
			
			//搜索快车行程起点周围的司机
			List<Driver> driverList = driverService.searchByDistance("", driverQuery, 10000, pageable);

			//周围没有司机
			if(driverList == null || driverList.isEmpty())
				return;

			//推送行程给司机
			List<String> aliasList = new ArrayList<String>();
			String toUser = "";
			
			String userTripNo = userTrip.getTripNo();
			for (Driver driver : driverList) {

				String driverOpenId = driver.getOpenId();
				if(StringUtils.isBlank(driverOpenId))
					continue;
				//判断是否推送过个这个司机
				PushMessage query = new PushMessage();
				query.setToUser(driverOpenId);
				query.setTripNo(userTripNo);
				
				long count = pushMessageService.selectCount("", query);
				if(count > 0) continue;
				
				aliasList.add(driverOpenId);
				toUser += "," + driverOpenId;
				//同时添加司机可以接单的行程记录
				DriverReceiveTrip receiveTrip = new DriverReceiveTrip();
				receiveTrip.setDriverOpenId(driverOpenId);
				receiveTrip.setTripNo(userTripNo);
				try{
					count = driverReceiveTripService.selectCount("", receiveTrip);
					if(count > 0) continue;
					driverReceiveTripService.insert("", receiveTrip);
				}
				catch(Exception e){
					LOGGER.error("driverReceiveTripService.insert.error="+e.getMessage());
				}
			}
			if(!StringUtils.isBlank(toUser)){
				toUser = toUser.substring(1);
			}
			String serviceCode = userTrip.getServiceCode();
			String moduleName = ServiceCode.getCodeName(serviceCode);
			String message = String.format("用户%s新行程",moduleName);
			String content = String.format("用户[%s]%s-%s新行程", moduleName, userTrip.getStartLocation(), userTrip.getEndLocation());
			PushCustomMsg customMsg = new PushCustomMsg();
			customMsg.setModule(serviceCode);
			customMsg.setType(CustomMsgType.UserNewTrip.getValue());
			customMsg.setKey(userTripNo);

			PushMessage pushMessage = new PushMessage();
			pushMessage.setContent(content);
			pushMessage.setModule(serviceCode);
			pushMessage.setTitle(message);
			pushMessage.setTripNo(userTripNo);
			pushMessage.setToUser(toUser);
			pushMessage.setMessage(message);
			pushMessage.setContent(content);
			pushMessage.setExtras(customMsg.toJson());
			pushMessage.setPushTime(SystemUtil.getAllSeconds());
			pushMessage.setJumpUrl("");
			pushMessageService.pushToUser("", pushMessage, PushTargetApp.Driver, customMsg, aliasList, true);
		}
		catch(Exception e){
			LOGGER.error("KCUserTripPushToDriverThread.error="+e.getMessage());
		}
	}
}
